--
-- Author: mm
-- Date: 2019-05-15 16:17:15
--
-- CrossServerController
--

local CrossServerView = import("..views.CrossServerView")

local fileName = string.getModeName(...)
local CrossServerController = class(fileName, app.mvc.ControllerBase)
local CrossServerGuildWarHelper = require("app.crossServer.views.guildWar.CrossServerGuildWarHelper")


function CrossServerController:ctor(data)
    self.super.ctor(self, data)
end

function CrossServerController:init()
    -- self._itemChangeHandler = UD:onChanged("bag", handler(self, self.updateItem))
end

function CrossServerController:removeListeners()
    self.super.removeListeners(self)
    -- UD:unChanged("bag", self._itemChangeHandler)
end

function CrossServerController:onEvent(eventName, data)
    if eventName == "onMapChanged" then
        if UD.csGuildWarData:isCSGuildWarPrepareMap() then
            self:openGuildWarBattleScorePanel()
        else
            self:closeGuildWarBattleScorePanel()
        end
    end
end

function CrossServerController:listEvent()
    return {
        "onMapChanged",
    }
end


function CrossServerController:openView(data, secondTabIndex, otherParams)
    if nil == self._view then
        self._view = CrossServerView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._view, data)
    if type(data) == "table" then
        self._curIndex = data.type
    else
        self._curIndex = data
    end
    self._view:initWithDefaultTab(self._curIndex)
    if self._curIndex == Enums.CrossServerTab.GuildWar then
        if secondTabIndex then
            local guildWarPanel = self._view:getPanelByKey(Enums.CrossServerTab.GuildWar, true)
            if guildWarPanel then
                guildWarPanel:setSelectedByIndex(secondTabIndex)
            end
            -- 显示挑战 tip
            if otherParams then
                if secondTabIndex == 5  then
                    local renderIndex = tonumber(otherParams)
                    if renderIndex <= 4 or renderIndex >= 1 then
                        EM:notify(ViewEvent.CSGuildWar_ShowChallengeTips, renderIndex)
                    end
                elseif secondTabIndex == 3 then
                    local rank = tonumber(otherParams)
                    self:popGuildWarMemberAward(rank)
                end
            end
        end
    -- elseif self._curIndex == Enums.CrossServerTab.Elite then
    --     if secondTabIndex then
    --         -- 显示当前活动的tips
    --         local panel = self._view:getPanelByKey(Enums.CrossServerTab.Elite)
    --         if panel then
    --             panel:isShowTips(true)
    --         end
    --     end
    -- elseif self._curIndex == Enums.CrossServerTab.Shop then
    --     -- 奇迹商店
    --     local panel = self._view:getPanelByKey(Enums.CrossServerTab.Shop)
    --     if panel then
    --         panel:setSelectItemInfo({data.itemType, data.itemId})
    --         panel:updateLeftPanel()
    --     end
    end
    self._view:show()
end

function CrossServerController:closeView(data)
    if self._view then
        self:removeChild(self._view, data)
        self:clearView()
    end
end
function CrossServerController:clearView()
    if self._view then
        self._view = nil
    end
end


--==================================================
        --跨服公会战
--==================================================
-------------------------------------------------------
--弹出战报 界面
function CrossServerController:popGuildWarFightReport(matchId)
    UD.csGuildWarData:C2S_WorldGuildWarReport(matchId, function(reportInfo)
        if reportInfo then
            local datas = reportInfo:getReports()
            if datas and #datas > 0 then
                local GuildWarFightReportPop = require("app.crossServer.views.guildWar.pops.GuildWarFightReportPop")
                local view = GuildWarFightReportPop.new(nil)
                view:onCloseView(function(sender)
                    UIHandler:removePopupUiByKey(sender:getViewName())
                end)
                -- view:requestFightReport(matchId)
                -- view:onOperate(handler(self, self.onOperate))
                UIHandler:addPopupUi(view, view:getViewName())
                view:show()
            else
                display.pushToast(L("WGWNoBattleReport"))
            end
        end
    end)
end

--弹出成员战功 
function CrossServerController:popGuildWarMemberAward(guildRank)
    if not guildRank then
        guildRank = 1
    end
    local GuildWarGuildMemberAwardPop = require("app.crossServer.views.guildWar.pops.GuildWarGuildMemberAwardPop")
    local view = GuildWarGuildMemberAwardPop.new(nil, data)
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    -- view:onOperate(handler(self, self.onOperate))
    view:setRank(guildRank)
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end

--弹出 战绩榜
function CrossServerController:popGuildWarGuildScore(data)

    UD.csGuildWarData:C2S_WorldGuildWarRank(function(datas)
        if datas and #datas > 0 then
            local GuildWarGuildScorePop = require("app.crossServer.views.guildWar.pops.GuildWarGuildScorePop")
            local view = GuildWarGuildScorePop.new(nil, data)
            view:onCloseView(function(sender)
                UIHandler:removePopupUiByKey(sender:getViewName())
            end)
            view:setData(datas)
            -- view:onOperate(handler(self, self.onOperate))
            UIHandler:addPopupUi(view, view:getViewName())
            view:show()
        else
            display.pushToast(L("duelGametips4"))
        end
    end)
end


function CrossServerController:openGuildWarReadMe(data)
    local GuildWarReadMeView = require("app.crossServer.views.guildWar.GuildWarReadMeView")
    local view = GuildWarReadMeView.new(app.mvc.VIEWTYPE_BLUR)
    local viewName = view:getViewName()
    view:onCloseView(function(sender)
        UIHandler:removeSystemUiByKey(viewName)
    end)
    UIHandler:addSystemUi(view, viewName)
    view:show()
end

-- 战斗准备地图  计分板
function CrossServerController:openGuildWarBattleScorePanel()
    if UD.csGuildWarData:isCSGuildWarPrepareMap()  then
        EM:notify(ViewEvent.CSGuildWar_ShowGuildWarScorePanelToDesktop)
    end
end

function CrossServerController:closeGuildWarBattleScorePanel()
    EM:notify(ViewEvent.CSGuildWar_CloseGuildWarScorePanelToDesktop)
end

function CrossServerController:popGuildWarBattleWin()
    local GuildWarBattleWinPop = require("app.crossServer.views.guildWar.pops.GuildWarBattleWinPop")
    local view = GuildWarBattleWinPop.new(nil)
    view:onCloseView(function(sender)
        if UD.csGuildWarData:isCSGuildWarPrepareMap() then
            UD.csGuildWarData:C2S_LeaveWorldGuildWarMap()
        end
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()

end

function CrossServerController:popGuildWarBattleLose()
    local GuildWarBattleLosePop = require("app.crossServer.views.guildWar.pops.GuildWarBattleLosePop")
    local view = GuildWarBattleLosePop.new(nil)
    view:onCloseView(function(sender)
        if UD.csGuildWarData:isCSGuildWarPrepareMap() then
            UD.csGuildWarData:C2S_LeaveWorldGuildWarMap()
        end
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end

function CrossServerController:popGuildWarFightOpenTip()
    local GuildWarFightOpenTipPop = require("app.crossServer.views.guildWar.pops.GuildWarFightOpenTipPop")
    local view = GuildWarFightOpenTipPop.new(nil)
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    UIHandler:addPopupUi(view, view:getViewName())
    view:show()
end


function CrossServerController:openGuildWarViewPanelByIndex(index)
    if self._view then
        self._view:setTabSelectedByKey(Enums.CrossServerTab.GuildWar)
        local guildWarPanel = self._view:getPanelByKey(Enums.CrossServerTab.GuildWar, true)
        if guildWarPanel then
            guildWarPanel:setSelectedByIndex(index)
        end
    else
        app:sendMsg("CrossServerController", "openView", Enums.CrossServerTab.GuildWar, index)
    end
end

function CrossServerController:showJoinTips()
    local rank = UD.csGuildWarData:getMyInGuildPowerRank()
    local memberNum = UD.csGuildWarData:getMyGuildMemberNum()
    if not rank or not memberNum then
        return
    end

    local colorNormal = cc.c3b(255, 225, 132)
    local colorGreenBlue = cc.c3b(0, 220, 220)
    local colorRed = cc.c3b(255, 0, 0)
    local colorGreen = cc.c3b(0, 220, 0)
    -- local colorGray = cc.c3b(160, 160, 160)
    -- PartnerRelationScoreTipTitle = "升级羁绊点品质以提升羁绊评级"
    local contents = {}
    local part1 = {}
    table.insert(part1, {text = L("qualificationTips_1"), color = colorNormal})
    table.insert(contents, part1)

    local part2 = {}
    table.insert(part2, {text = L("qualificationTips_2"), color = colorNormal})
    table.insert(contents, part2)

    local part3 = {}
    table.insert(part3, {text = L("qualificationTips_3"), color = colorGreenBlue})
    table.insert(part3, {text = L("qualificationTips_4"), color = colorGreenBlue})
    table.insert(contents, part3)

    -- local totalNum = 0
    -- local newguildModel = app:getInst("NewguildModel")
    -- if newguildModel then
    --     local guildInfo = newguildModel:getMyGuildInfo()
    --     if guildInfo then
    --         totalNum = guildInfo:getGuildMemberNum()
    --     end
    -- end

    local part4 = {}
    table.insert(part4, {text = string.format(L("qualificationTips_5"), memberNum), color = colorNormal})
    if CrossServerGuildWarHelper.isSafeGetTicket(rank) then
        table.insert(part4, {text = string.format(L("qualificationTips_6"), rank), color = colorGreen})
    else
        table.insert(part4, {text = string.format(L("qualificationTips_7"), rank), color = colorRed})
    end
    table.insert(contents, part4)

    local param = {tipsType = Enums.TipsType.contents,contents = contents, closeCallBack = function() app:dispatchCustomEvent("EVENT_CLOSE_FLOAT") end}
    UIHandler:setLastTouchPositionOffset(-50, 0)
    app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=param})
end


-- function CrossServerController:updateItem()
--     if cc.isDead(self._view) then return end

--     local panel = self._view:getPanelByKey(Enums.CrossServerTab.Shop)
--     if panel and panel:isVisible() then
--         panel:updateGoldInfo()
--     end
-- end

return CrossServerController
